<?php
require_once('db.php');
class Student{
  public function login($studentId,$studentPwd){
    $conn=getConn();
    $sql="select * from student where studentId=".$studentId;
    $result=mysqli_query($conn,$sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        if($row['studentPwd']==$studentPwd){
          Session_start();
          $_SESSION['studentName']=$row['studentName'];
          $_SESSION['studentId']=$studentId;
          $_SESSION['loginFlag']=true;
          mysqli_close($conn);
          return true;
        }else{
          mysqli_close($conn);
          return false;
        }
    }
  }
  public function loginOut(){
    Session_start();
    session_destroy(); 
  }
  public function insert($studentId,$studentName,$studentPwd){
    $conn=getConn();
    $sql="insert into student (studentId,studentName,studentPwd ) values (".$studentId.",'".$studentName."','".$studentPwd."')";
    $result=mysqli_query($conn,$sql);
    mysqli_close($conn);
    return $result;    
  }
  public function showAll(){
    $conn=getConn();
    $sql="select * from student";
    $result = mysqli_query($conn,$sql);
    $resArr=array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $temp='{"id":'.$row['id'].',"studentId":'.$row['studentId'].',"studentName":"'.$row['studentName'].'","studentPwd":'.$row['studentPwd'].'}';
      array_push($resArr,$temp);
    }
    mysqli_close($conn);
    return $resArr;    
  } 
  public function del($id){
    $conn=getConn();
    $sql="delete from student where id=".$id;
    $result=mysqli_query($conn,$sql);
    mysqli_close($conn);
    return $result;
  }
}


?>